package org.apache.commons.logging.impl;

import java.util.Date;

import com.google.gwt.core.client.GWT;

public abstract class AbstractLogger implements GWTLog {
	
	protected final static boolean isScript = GWT.isScript();	
	protected String loggerName = null;

	protected final String getName() {
		return loggerName;
	}
	
	protected StackTraceElement getStackTraceElement() {
		Throwable dummyException = new Throwable();
		StackTraceElement locations[] = dummyException.getStackTrace();
		StackTraceElement caller = null; 
		if (locations != null && locations.length > 4) {
			caller = locations[4];
		}
		
		return caller;
	}

	protected void gwtShellLog(String level, Object msg, Throwable ex) {
		if(!isScript) {
			StackTraceElement locations = getStackTraceElement();
			String method = "";
			if (locations != null) {
				method = "."+locations.getMethodName();
			}
			
			String message = "<"+new Date().toString() + "> <" + getName() + method + ">: " + msg;
			GWT.log(message, ex);			
		}
	}
	

}
